package com.furns.web;

import com.furns.entity.Furn;
import com.furns.entity.Page;
import com.furns.service.FurnService;
import com.furns.service.impl.FurnServiceImpl;
import com.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author LuoJiaCheng
 * @ID 2240129126
 * @Version 1.0
 */
@WebServlet(name = "CustomerFurnServlet", value = "/CustomerFurnServlet")
public class CustomerFurnServlet extends BasicServlet {
    //定义属性
    private FurnService furnService = new FurnServiceImpl();

    /**
     * 主页分页导航请求
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //调用service对象获取Page对象
        Page<Furn> page = furnService.page(pageNo, pageSize);
        req.setAttribute("page", page);
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);

    }

    /**
     * 家具名搜索栏
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //1.如果参数有name无值，接收“”
        //2.如果name都没有，接收到的null
        //3.把null和“”合并
        String name = req.getParameter("name");
        if (null == name) {
            name = "";
        }

        //调用service对象获取Page对象
        Page<Furn> page = furnService.pageByName(name, pageNo, pageSize);
        StringBuilder url = new StringBuilder("CustomerFurnServlet?action=pageByName");
        if (!"".equals(name)) {
            url.append("&name=").append(name);
        }
        page.setUrl(url.toString());
        req.setAttribute("page", page);
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);

    }
}
